/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.serverpackets;

import com.aionemu.gameserver.model.team.alliance.PlayerAlliance;
import com.aionemu.gameserver.model.team.group.LootGroupRules;
import com.aionemu.gameserver.network.aion.AbstractAionServerPacket;
import com.aionemu.gameserver.network.aion.GameClient;

/**
 * @author Lyahim, Sarynth (Thx Rhys2002 for Packets)
 *
 */
public class SM_ALLIANCE_INFO extends AbstractAionServerPacket<GameClient>
{
	private final PlayerAlliance	alliance;

	public SM_ALLIANCE_INFO(PlayerAlliance alliance)
	{
		this.alliance = alliance;
	}

	@Override
	protected void writeImpl(GameClient client)
	{
		writeH(4); //WTF NcSoft...... Only work if alliance have 4 groups...
		writeD(alliance.getObjectId());
		writeD(alliance.getLeader().getLeaderObjectId());

		int i = 0;
		for (int group : alliance.getVicCaptains())
		{
			writeD(group);
			i++;
		}
		for (;i<4 ; i++)
		{
			writeD(0);
		}
		LootGroupRules lgr = alliance.getLootGroupRules();
		writeD(lgr.getLootRule().getId()); //loot rule type - 0 freeforall, 1 roundrobin, 2 leader
		writeD(lgr.getAutodistribution()); //autoDistribution - 0 or 1
		writeD(lgr.getCommonItemAbove()); //this.common_item_above); - 0 normal 2 roll 3 bid
		writeD(lgr.getSuperiorItemAbove()); //this.superior_item_above); - 0 normal 2 roll 3 bid
		writeD(lgr.getHeroicItemAbove()); //this.heroic_item_above); - 0 normal 2 roll 3 bid
		writeD(lgr.getFabledItemAbove()); //this.fabled_item_above); - 0 normal 2 roll 3 bid
		writeD(lgr.getEthernalItemAbove()); //this.ethernal_item_above); - 0 normal 2 roll 3 bid
		writeD(lgr.getOverEthernal()); //this.over_ethernal); - 0 normal 2 roll 3 bid
		writeD(lgr.getOverOverEthernal()); //this.over_over_ethernal); - 0 normal 2 roll 3 bid
		writeC(0); //unk
		writeD(0); //unk

		for (i = 0; i< 4; i++)
		{
			writeD(i);
			writeD(1000+i);
		}

		writeD(0); //System message ID
		writeS(""); //System message
		if (alliance.getLeague() != null)
		{
			lgr = alliance.getLeague().getLootGroupRules();
			writeH(alliance.getLeague().size());
			writeD(lgr.getLootRule().getId()); //loot rule type - 0 freeforall, 1 roundrobin, 2 leader
			writeD(lgr.getAutodistribution()); //autoDistribution - 0 or 1
			writeD(lgr.getCommonItemAbove()); //this.common_item_above); - 0 normal 2 roll 3 bid
			writeD(lgr.getSuperiorItemAbove()); //this.superior_item_above); - 0 normal 2 roll 3 bid
			writeD(lgr.getHeroicItemAbove()); //this.heroic_item_above); - 0 normal 2 roll 3 bid
			writeD(lgr.getFabledItemAbove()); //this.fabled_item_above); - 0 normal 2 roll 3 bid
			writeD(lgr.getEthernalItemAbove()); //this.ethernal_item_above); - 0 normal 2 roll 3 bid
			writeD(lgr.getOverEthernal()); //this.over_ethernal); - 0 normal 2 roll 3 bid
			writeD(lgr.getOverOverEthernal()); //this.over_over_ethernal); - 0 normal 2 roll 3 bid
			i = 0;
			for (PlayerAlliance alli : alliance.getLeague().getMembers())
			{
				writeD(i++);
				writeD(alli.getLeaderObjectId());
				writeD(alli.getMembers().size());
				writeS(alli.getName());
			}
		}
	}
}
